package structure.linkedList;

import java.util.Comparator;
import java.util.PriorityQueue;

public class LeetCode_23_4 {

    public static void main(String[] args) {
        String str = "abs";
        String str1 = "v";
        System.out.println(str.indexOf(str1));
    }


    /**
     * 优先队列解题
     *
     * @param lists
     * @return
     */
    public ListNode mergeKLists(ListNode[] lists) {

        if (lists == null || lists.length == 0) {
            return null;
        }
        PriorityQueue<ListNode> queue = new PriorityQueue<>(new Comparator<ListNode>() {
            @Override
            public int compare(ListNode o1, ListNode o2) {
                if (o1.val < o2.val) {
                    return -1;
                } else if (o1.val > o2.val) {
                    return 1;
                } else {
                    return 0;
                }
            }
        });

        for (ListNode node : lists) {
            if (node != null) {
                queue.add(node);
            }
        }

        ListNode prev = new ListNode(0);
        ListNode cur = prev;
        while (!queue.isEmpty()) {
            ListNode node = queue.poll();
            cur.next = node;
            if (node.next != null) {
                queue.add(node.next);
            }
            cur = cur.next;
        }
        return prev.next;

    }
}
